import * as Cesium from "cesium";
class DynamicFlowMaterialProperty {
  color: Cesium.Color;
  speed: number;
  opacity: number;
  percent: number;
  _definitionChanged: any;
  constructor(
    color: Cesium.Color,
    speed: number,
    opacity: number,
    percent: number
  ) {
    this._definitionChanged = new Cesium.Event();
    this.color = color;
    this.speed = speed;
    this.opacity = opacity;
    this.percent = percent;
    this.init(color, speed, opacity, percent);
  }
  get isConstant() {
    return false;
  }
  get definitionChanged() {
    return this._definitionChanged;
  }
  getType() {
    return "DynamicFlow";
  }
  getValue(_time: any, result: any) {
    if (!Cesium.defined(result)) {
      result = {};
    }
    result.color = this.color;
    result.speed = this.speed;
    result.opacity = this.opacity;
    result.percent = this.percent;
    return result;
  }
  equals() {
    return false;
  }
  init(color: Cesium.Color, speed: number, opacity: number, percent: number) {
    const source = `
    uniform vec4 color;
    uniform float speed;
    uniform float percent;
    uniform float opacity;
    czm_material czm_getMaterial(czm_materialInput materialInput)
        {
            czm_material material = czm_getDefaultMaterial(materialInput);
            vec2 st = materialInput.st;
            float time = fract( czm_frameNumber  *  speed / 1000.0);
            time *= (1.0 + percent);
            float alpha = smoothstep(time- percent, time, st.s) * step(-time, -st.s);
            alpha += opacity;
            material.diffuse= color.rgb;
            material.alpha = alpha;
            return material;
        }
        `;
    const uniforms = {
      color,
      speed,
      opacity,
      percent,
    };
    (Cesium.Material as any)._materialCache.addMaterial("DynamicFlow", {
      fabric: {
        type: "DynamicFlow",
        uniforms,
        source,
      },
      translucent: true,
    });
  }
}
export default DynamicFlowMaterialProperty;
